'use strict';
/**
 * Created by FreezeSoul on 2015/9/25.
 */
define([
        'angular',
        'localforage',
        'app/modules/editor/define'
    ],
    function(angular,localforage) {
        angular.module('ad.editor')
            .directive('adEditorHeaderDirective',['$rootScope','$state','Notification',function($rootScope,$state,Notification){
                return {
                    templateUrl:'app/modules/editor/views/header.tpl.html',
                    restrict: 'E',
                    replace: true,
                    link:function($scope, $element,$attrs){
                        $scope.pathInfo = "首页";
                        $scope.goHome = function(){
                            $scope.pathInfo = "首页";
                            $state.go('editor.home');
                            return false;
                        };
                        $scope.exitToLogin = function(){
                            $rootScope.currentUser = null;
                            $state.go('login');
                            return false;
                        };
                        $scope.goToSetting = function(){
                            $scope.pathInfo = "关于";
                            $state.go('editor.setting');
                            return false;
                        };
                        $scope.goToPlay = function () {
                            $state.go('player');
                            return false;
                        };

                        $scope.clearCache = function () {
                            var localforageInstance = localforage.createInstance({
                                driver: localforage.INDEXEDDB,
                                name: "amazing-dashboard"
                            });
                            localforageInstance.dropInstance().then(function () {
                                Notification.success('清除缓存成功！请关闭浏览器重新打开页面！');
                            });
                        };

                        var unbind = $rootScope.$on('ad.message.pathinfo', function(event, arg){
                            $scope.pathInfo = arg;
                        });
                        $scope.$on('$destroy', unbind);
                    }
                }
            }]);
    });
